//
//  MainTabBarViewController.swift
//  SinaWeibo
//
//  Created by Arvin on 15/11/8.
//  Copyright © 2015年 Arvin. All rights reserved.
//

import UIKit

class MainTabBarViewController: UITabBarController, MainTabBarDelegate {

    func addComposeController() {
        let navi = UINavigationController(rootViewController: ComposeViewController())
        self.presentViewController( navi, animated: true, completion: nil)
    }

    override func viewDidLoad() {
         super.viewDidLoad()
        let mainTab = MainTabBar()
        mainTab.composeDelegate = self
        // KVC赋值, 给系统的tabBar. KVC可以改任何一个(包刮系统的)属性的值
        self.setValue(mainTab, forKey: "tabBar")
        
        addChildController()
    }
    
    private func addChildController() {
        let home = HomeTableViewController()
        addChildViewController(home, title: "首页", image: "tabbar_home")
        
        let message = MessageTableViewController()
        addChildViewController(message, title: "消息", image: "tabbar_message_center")
        
        let discover = DiscoverTableViewController()
        addChildViewController(discover, title: "发现", image: "tabbar_discover")
        
        let profile = ProfileTableViewController()
        addChildViewController(profile, title: "我", image: "tabbar_profile")
    }
    
    private func addChildViewController(vc: UIViewController, title: String, image: String) {
        vc.title = title
        let nav = UINavigationController(rootViewController:vc)
        vc.tabBarItem.image = UIImage(named: image)?.imageWithRenderingMode(.AlwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: (image + "_highlighted"))?.imageWithRenderingMode( .AlwaysOriginal)
        // 在UIAppDelegate中进行全局的设置
//        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState:UIControlState.Selected)
        addChildViewController(nav)
    }
}
